package com.cadre.system.service.impl;

import com.cadre.common.utils.common.GenerateUUID;
import com.cadre.common.utils.oldutil.ExcelImpotUtils;
import com.cadre.system.domain.officer.A01;
import com.cadre.system.mapper.ExcelImpMapper;
import com.cadre.system.service.ExcelImpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("excelImpService")
/* loaded from: ExcelImpServiceImpl.class */
public class ExcelImpServiceImpl implements ExcelImpService {
    @Autowired
    ExcelImpMapper excelImpMapper;
    private static String Status1 = "1";
    private static String Status2 = "2";
    private static String Status3 = "3";

    @Override // com.tdqy.cadre.modular.profess.service.ExcelImpService
    public String readExcelInTempTable(MultipartFile file, String impTable, String tempTable, Map<String, String> excelAndTempTable, String tablename) {
        int start = 0;
        if (tablename.equals("A91")) {
            start = 0;
        }
        this.excelImpMapper.emptyTable(tempTable);
        try {
            List<Map<Integer, String>> list = ExcelImpotUtils.readExcel(file);
            for (int i = start; i < list.size(); i++) {
                List<Map<String, String>> newList = new ArrayList<>();
                Map<String, String> newMap = new HashMap<>();
                if (tablename.equals("A91") && !list.get(i).get(0).toString().equals("") && !list.get(i).get(1).toString().equals("")) {
                    newList = getPersonidAndStatus(list.get(i));
                }
                int n = 0;
                while (n < newList.size()) {
                    Map<String, String> newMap2 = newMap;
                    for (Map.Entry<String, String> entry : excelAndTempTable.entrySet()) {
                        Map<String, String> newMap3 = newList.get(n);
                        newMap3.put(entry.getValue().toUpperCase(), list.get(i).get(Integer.valueOf(Integer.parseInt(entry.getKey()))));
                        newMap2 = newMap3;
                    }
                    String str = getCount(tablename, newMap2, impTable, tempTable);
                    if (!str.equals("")) {
                        newMap2.put("a9101a", str);
                        newMap2.put("status", Status2);
                    }
                    newMap2.put("flowid", GenerateUUID.getUUID());
                    this.excelImpMapper.insertTempTable(newMap2, tempTable);
                    n++;
                    newMap = newMap2;
                }
            }
            return "操作成功";
        } catch (IOException e) {
            e.printStackTrace();
            return "操作成功";
        }
    }

    private String getCount(String tablename, Map<String, String> newMap, String impTable, String tempTable) {
        String str = "";
        if (tablename.equals("A91") && newMap.get("status").equals("1") && newMap.get("A9102") != null && newMap.get("personid") != null) {
            str = this.excelImpMapper.selectForMap(newMap.get("A9102"), newMap.get("personid"), impTable, tempTable);
        }
        if (str == null) {
            return "";
        }
        return str;
    }

    private List<Map<String, String>> getPersonidAndStatus(Map<Integer, String> map) {
        List<Map<String, String>> mapList = new ArrayList<>();
        Map<String, String> newMap = new HashMap<>();
        String a9101 = map.get(0);
        String a9102 = map.get(1);
        List<A01> list = this.excelImpMapper.selectA01List(a9101, a9102);
        if (list.size() < 1) {
            newMap.put("name", a9101);
            newMap.put("A9102", a9102);
            newMap.put("status", Status3);
            mapList.add(newMap);
        } else if (list.size() > 1) {
            List<A01> list2 = this.excelImpMapper.selectA01List(a9101, a9102);
            if (list2.size() == 1) {
                for (int i = 0; i < list.size(); i++) {
                    newMap.put("A0183", list2.get(i).getA0183());
                    newMap.put("status", Status1);
                    newMap.put("personid", list2.get(i).getFlowid());
                    newMap.put("name", list2.get(i).getA0101());
                    mapList.add(newMap);
                }
            } else {
                for (int n = 0; n < list2.size(); n++) {
                    newMap.put("A0183", list2.get(n).getA0183());
                    newMap.put("status", Status3);
                    newMap.put("personid", list2.get(n).getFlowid());
                    newMap.put("name", list2.get(n).getA0101());
                    mapList.add(newMap);
                }
            }
        } else {
            newMap.put("A0183", list.get(0).getA0183());
            newMap.put("status", Status1);
            newMap.put("personid", list.get(0).getFlowid());
            newMap.put("name", list.get(0).getA0101());
            mapList.add(newMap);
        }
        return mapList;
    }

    @Override // com.tdqy.cadre.modular.profess.service.ExcelImpService
    public List<Map<String, Object>> getListForStatus(String TempTable, String status) {
        return this.excelImpMapper.getListForStatus(TempTable, status);
    }

    @Override // com.tdqy.cadre.modular.profess.service.ExcelImpService
    @Transactional(rollbackFor = {Exception.class})
    public int deleteByIds(String tempTable, List<String> asList) {
        return this.excelImpMapper.deleteByIds(tempTable, asList);
    }

    @Override // com.tdqy.cadre.modular.profess.service.ExcelImpService
    @Transactional(rollbackFor = {Exception.class})
    public int insertForTempTable(String impTable, String tempTable, String status, Map<String, String> map) {
        return this.excelImpMapper.insertForTempTable(impTable, tempTable, status, map);
    }

    @Override // com.tdqy.cadre.modular.profess.service.ExcelImpService
    @Transactional(rollbackFor = {Exception.class})
    public String updateImpTable(String impTable, String tempTable, String thisIds, String tableName) {
        String[] strArr = thisIds.split(",");
        for (int i = 0; i < strArr.length; i++) {
            if (strArr[i] != "") {
                Map<String, Object> map = this.excelImpMapper.selectTempByFlowId(tempTable, strArr[i]);
                if (tableName.equals("A91")) {
                    this.excelImpMapper.updateA91(map.get("A9101").toString(), map.get("A9102").toString(), map.get("PERSONID").toString());
                }
            }
        }
        return "操作成功";
    }
}
